//
//  file.m
//  SrnTeacher
//
//  Created by iDeneb on 5/6/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "file.h"


@implementation file

-(NSString *) read {
	// Get home directory
	NSString *homeDir = NSHomeDirectory();
	
	// Get temporary directory
	NSString *tempDir = NSTemporaryDirectory();
	
	// Get documents directory
	NSArray *arrayPaths = 
	NSSearchPathForDirectoriesInDomains(
										NSDocumentDirectory,
										NSUserDomainMask,
										YES);
	
	NSString *docDir = [arrayPaths objectAtIndex:0];
	
	/*NSString *myFilePath = [[NSBundle mainBundle]
							pathForResource:@"Results"
							ofType:@"csv"];
	*/
	NSString *myFilePath = [self getPath];
	NSString *myFileContents = [NSString stringWithContentsOfFile:myFilePath
														 encoding:NSUTF8StringEncoding
															error:nil];
	
	// Format output
	NSString *s = 
	[NSString stringWithFormat:@"homeDir:\n"
	 @"%@\n"
	 @"tempDir:\n"
	 @"%@\n"
	 @"docDir:\n"
	 @"%@\n"
	 @"myFilePath:\n"
	 @"%@\n"
	 @"Contents of file:\n"
	 @"%@\n",
	 homeDir,
	 tempDir,
	 docDir,
	 myFilePath,
	 myFileContents];		
	
	// Create pathname to Documents directory
	NSString *newFilePath = [docDir stringByAppendingString:@"/NewFile.txt"];
	
	// Write string to file
	[s writeToFile:newFilePath
		atomically:YES
		  encoding:NSUTF8StringEncoding
			 error:nil];
}

-(NSString *) getPath{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	// the path to write file
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"results.csv"];
	return appFile;
}
-(BOOL) write:(NSString *)content{
	
	
	//NSString *myFilePath = [[NSBundle mainBundle]
	//						pathForResource:@"results"bai
	//						ofType:@"csv"];
	//NSString  *paths = [[NSBundle mainBundle] resourcePath];
	
	//NSString *myFilePath = [paths stringByAppendingString:@"results.csv"];
	NSString *myFilePath = [self getPath];	
	//NSString *outpath = @"results.csv";
	NSFileManager *fm = [NSFileManager defaultManager];
    BOOL find = [fm fileExistsAtPath:myFilePath];
	if (!find) {
		BOOL issuccuss = [fm createFileAtPath:myFilePath contents:nil attributes:nil];
		if (!issuccuss) {
			NSLog(@"results.csv create failed!");
		}
	}
	NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:myFilePath];
	[myHandle seekToEndOfFile];
	NSData *theData = [content dataUsingEncoding:NSUTF8StringEncoding];	
	[myHandle writeData:theData];
	[myHandle closeFile];	
	//return [content writeToFile:@"/Users/ideneb/Documents/out.csv"
	//		  atomically:YES
	//			encoding:NSUTF8StringEncoding
	//			   error:nil];
	
}

-(BOOL) deleteFile{
	NSFileManager *fm = [NSFileManager defaultManager];
	//NSString  *paths = [[NSBundle mainBundle] resourcePath];
	//NSString *myFilePath = [paths stringByAppendingString:@"results.csv"];
    NSString *myFilePath = [self getPath];
	BOOL find = [fm fileExistsAtPath:myFilePath];
	if (find) {
		[fm removeItemAtPath:myFilePath error:nil];
	}
}

@end
